﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Skywave.Data
{
    [DataContract]
    public class ChartDataGeneratorAxisString : ChartDataGeneratorAxis
    {

        private Dictionary<string, string> _Design_ColumnsAvailable = new Dictionary<string, string>();
        /// <summary>
        /// Key: a unique id for column, Value: display text for client
        /// </summary>
        public Dictionary<string, string> Design_ColumnsAvailable
        {
            get { return _Design_ColumnsAvailable; }
            set { _Design_ColumnsAvailable = value; }
        }

        private List<string> _Design_ColumnsSelected = new List<string>();
        /// <summary>
        /// Design_ColumnsSelected Property
        /// </summary>
        [DataMember]
        public List<string> Design_ColumnsSelected
        {
            get { return _Design_ColumnsSelected; }
            set { _Design_ColumnsSelected = value; }
        }

        private List<string> _ColumnSQLs = new List<string>();
        /// <summary>
        /// ColumnSQLs Property
        /// </summary>
        public List<string> ColumnSQLs
        {
            get { return _ColumnSQLs; }
            set { _ColumnSQLs = value; }
        }

        public override ChartDataGeneratorAxisCases GetCases(string prefixForNames, ChartDataGenerator parent)
        {
            string column_Primary_Name = parent.Filter_PrimaryColumn_FullName;
            if (ColumnSQLs == null || ColumnSQLs.Count == 0)
                throw new ArgumentException("'ColumnSQLs' can not be null or empty, you should specify at least one column sql.");
            ChartDataGeneratorAxisCases r = new ChartDataGeneratorAxisCases();
            string s1, s2;
            List<string> colSQLs = (from x1 in ColumnSQLs select x1).ToList();
            //
            r.JoinClauses = JoinClauses.Values.ToList();
            foreach (var fe1 in JoinClauses)
            {
                s1 = string.Format("{0}t_{1}", prefixForNames, fe1.Key);
                r.JoinClauses = (from x1 in r.JoinClauses select x1.Replace(fe1.Key, s1)).ToList();
                colSQLs = (from x1 in colSQLs select x1.Replace(fe1.Key, s1)).ToList();
            }
            //
            r.Params = Params.Values.ToList();
            foreach (var fe1 in Params)
            {
                s1 = string.Format("{0}p_{1}", prefixForNames, fe1.Key);
                r.Params = (from x1 in r.Params select x1.Replace(fe1.Key, s1)).ToList();
                colSQLs = (from x1 in colSQLs select x1.Replace(fe1.Key, s1)).ToList();
            }
            //
            int iColumn = 1;
            foreach (string colSQL in colSQLs)
            {
                s2 = string.Format("{0}c_{1}", prefixForNames, iColumn);
                //s1 = string.Format("({0}) AS {1}", colSQL, s2);
                r.ColumnsSQLs.Add(s2, colSQL);
                //
                iColumn++;
            }
            //
            return r;
        }

        public override void CopySettingsFrom(ChartDataGeneratorAxis other)
        {
            if (other == null)
                return;
            if (other is ChartDataGeneratorAxisString)
            {
                ChartDataGeneratorAxisString otherTyped = other as ChartDataGeneratorAxisString;
                Design_ColumnsSelected = (otherTyped.Design_ColumnsSelected == null) ? null : otherTyped.Design_ColumnsSelected.ToList(); ;
                MaxCaseCount = (otherTyped.MaxCaseCount == null) ? null : (int?)otherTyped.MaxCaseCount.Value; ;
                PointTextSeperator = otherTyped.PointTextSeperator;
            }
        }

        public override void CopyTo(ChartDataGeneratorAxis target)
        {
            if (target == null)
                return;
            base.CopyTo(target);
            if (target is ChartDataGeneratorAxisString)
            {
                ChartDataGeneratorAxisString target_Typed = target as ChartDataGeneratorAxisString;
                target_Typed.ColumnSQLs = (this.ColumnSQLs == null) ? null : this.ColumnSQLs.ToList();
                target_Typed.Design_ColumnsAvailable = (this.Design_ColumnsAvailable == null) ? null : this.Design_ColumnsAvailable.ToDictionary<KeyValuePair<string, string>, string, string>(q => q.Key, q => q.Value);
                target_Typed.CopySettingsFrom(this);
            }
        }
        public override object Clone()
        {
            ChartDataGeneratorAxisString itemNew = new ChartDataGeneratorAxisString();
            CopyTo(itemNew);
            return itemNew;
        }
    }
}
